﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using RestSharp;
using WebApiDemo.Contract;
using WebApplicationDemo1.Contract;
using Ymatou.ServiceCenter;
using System.Configuration;

namespace ConsoleApplicationTest
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                //TestServiceCenterFacade();
                //TestAutoRequestIdRoad();
                //TestRequestIdRoad();
                //ApiDemoTestPostJsonResultStringTask();
                //ApiDemoTestPostJsonResultString();
                //ApiDemoTestPostJsonResultStringCallback();
                //ApiDemoTestPostJsonCallback();
                //ApiDemoTestPostJsonTask();
                //ApiDemoTestGet();
                //ApiDemoTestPost();
                //ApiDemoTestPostJson();
                //ApiDemoTestPut();
                //ApiDemoTestDelete();
                //ServiceConfig serviceConfig = ServiceConfigManager.GetServiceConfig("A", "B");

                //ServiceMetadata serviceMetadata = ServiceMetadataManager.GetServiceMetadata("webapi.ymatou.com", "api/Default/TestGet", Method.GET);

                //TestRpcClientConfigManager();
                //TestGetServiceConfig();
                //TestGetServiceMetadata();

                SaveServiceMetadatas();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            Console.Read();
        }

        static void TestRpcClientConfigManager()
        {
            while (true)
            {
                RpcClientConfig rpcClientConfig = RpcClientConfigManager.GetCurrentClientConfig();
                Console.WriteLine(rpcClientConfig.ToString());
                Thread.Sleep(1000);
            }
        }

        static void TestGetServiceMetadata()
        {
            while (true)
            {
                ServiceMetadata serviceMetadata = ServiceMetadataManager.GetServiceMetadata("webapi.ymatou.com", "api/Default/TestGet", Method.GET);
                Thread.Sleep(10000);
            }
            
        }


        static void SaveServiceMetadatas()
        {
            IList<ServiceMetadata> list = new List<ServiceMetadata>();
            for (int i = 0; i < 3; i++)
            {
                ServiceMetadata serviceMetadata = new ServiceMetadata();
                serviceMetadata.AppId = Guid.NewGuid().ToString();
                serviceMetadata.ApiPath = Guid.NewGuid().ToString();
                serviceMetadata.ApiName = Guid.NewGuid().ToString();
                serviceMetadata.HttpMethod = Method.POST.ToString();

                list.Add(serviceMetadata);
            }
            ServiceCenterFacade.SaveServiceMetadatas(list);
        }

        static void TestGetServiceConfig()
        {
            while (true)
            {
               TimeSpan ts =   RpcClientConfigManager.GetServiceMetadataAutoUpdateInterval();
                Console.WriteLine("ts:" +ts);
                
                string consumerAppId = ConfigurationManager.AppSettings["AppId"]; 
                string providerAppId = "provider1";
                Stopwatch sw = Stopwatch.StartNew();
                sw.Start();
                ServiceConfig serviceConfig = ServiceConfigManager.GetServiceConfig(consumerAppId, providerAppId);
                sw.Stop();
                Console.WriteLine(string.Format("GetServiceConfig耗时：{0} ms", sw.ElapsedMilliseconds));
                Console.WriteLine(serviceConfig==null?"null":serviceConfig.ToString());
                
                Thread.Sleep(1000);
            }
        }


        static void TestServiceCenterFacade()
        {
            string appId = "A";
            //DateTime lastUpdateTime=DateTime.Now.AddDays(-1);
            IList<ServiceConfig> serviceConfigs = ServiceCenterFacade.GetServiceConfigs(appId);


        }

        static void TestRequestIdRoad()
        {
            EmptyRequest request = new EmptyRequest();
            EmptyResponse response = ServiceClient.Execute<EmptyRequest, EmptyResponse>(request);
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("TestRequestIdRoad," + response.Result);
            Console.ResetColor();
        }

        static void TestAutoRequestIdRoad()
        {
            Task.Factory.StartNew(() =>
            {
                while (true)
                {
                    TestRequestIdRoad();
                    Thread.Sleep(10);
                }
            });
        }

        static void ApiDemoTestPostJson()
        {
            TestPostJsonRequest request = new TestPostJsonRequest
            {
                Id = Guid.NewGuid().ToString("N"),
                Age = 0,
                Name = "tom",
                Price = 888M
            };

            TestPostResponse response = ServiceClient.Execute<TestPostJsonRequest, TestPostResponse>(request);
            Console.WriteLine(string.Format("ApiDemoTestPostJson,Id:{0},Data:{1}", response.Id, response.Data));

        }

        static void ApiDemoTestPostJsonResultString()
        {
            TestPostJsonRequest request = new TestPostJsonRequest
            {
                Id = Guid.NewGuid().ToString("N"),
                Age = 20,
                Name = "tom",
                Price = 888M
            };

            string result = ServiceClient.Execute(request);

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("ApiDemoTestPostJsonResultString," + result);
            Console.ResetColor();
        }

        static void ApiDemoTestPostJsonResultStringCallback()
        {
            TestPostJsonRequest request = new TestPostJsonRequest
            {
                Id = Guid.NewGuid().ToString("N"),
                Age = 20,
                Name = "tom",
                Price = 888M
            };

            ServiceClient.ExecuteAsync(request, (result) =>
            {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine(result);
                Console.ResetColor();
            }, (ex) =>
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(string.Format("ApiDemoTestPostJsonResultStringCallback,Id:{0},Ex:{1},Callback", request.Id, ex));
                Console.ResetColor();
            });
        }

        static void ApiDemoTestPostJsonResultStringTask()
        {
            TestPostJsonRequest request = new TestPostJsonRequest
            {
                Id = Guid.NewGuid().ToString("N"),
                Age = 20,
                Name = "tom",
                Price = 888M
            };

            string result = ServiceClient.ExecuteTaskAsync<TestPostJsonRequest>(request).Result;

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine(string.Format("ApiDemoTestPostJsonResultStringTask,Id:{0},Data:{1},Task", request.Id, result));
            Console.ResetColor();
        }

        static void ApiDemoTestPostJsonCallback()
        {
            TestPostJsonRequest request = new TestPostJsonRequest
            {
                Id = Guid.NewGuid().ToString("N"),
                Age = 20,
                Name = "tom",
                Price = 888M
            };

            ServiceClient.ExecuteAsync<TestPostJsonRequest, TestPostResponse>(request, (response) =>
            {
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine(string.Format("ApiDemoTestPostJsonCallback,Id:{0},Data:{1},Callback", response.Id, response.Data));
                Console.ResetColor();
            }, (ex) =>
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(string.Format("ApiDemoTestPostJsonCallback,Id:{0},Ex:{1},Callback", request.Id, ex));
                Console.ResetColor();
            });

        }

        static void ApiDemoTestPostJsonTask()
        {
            TestPostJsonRequest request = new TestPostJsonRequest
            {
                Id = Guid.NewGuid().ToString("N"),
                Age = 20,
                Name = "tom",
                Price = 888M
            };

            TestPostResponse response = ServiceClient.ExecuteTaskAsync<TestPostJsonRequest, TestPostResponse>(request).Result;

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine(string.Format("ApiDemoTestPostJsonTask,Id:{0},Data:{1},Task", response.Id, response.Data));
            Console.ResetColor();
        }

        static void ApiDemoTestPost()
        {
            TestPostRequest request = new TestPostRequest
            {
                Id = Guid.NewGuid().ToString("N"),
                Age = 20,
                Name = "tom",
                Price = 888M
            };

            TestPostResponse response = ServiceClient.Execute<TestPostRequest, TestPostResponse>(request);
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine(string.Format("ApiDemoTestPost,Id:{0},Data:{1},Task", response.Id, response.Data));
            Console.ResetColor();
        }

        static void ApiDemoTestGet()
        {
            TestGetRequest request = new TestGetRequest
            {
                Id = Guid.NewGuid().ToString("N"),
                Age = 20,
                Name = "tom",
                Price = 888M
            };

            TestGetResponse response = ServiceClient.Execute<TestGetRequest, TestGetResponse>(request);

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine(string.Format("ApiDemoTestGet,Id:{0},Data:{1},Task", response.Id, response.Data));
            Console.ResetColor();
        }

        static void ApiDemoTestPut()
        {
            TestPutRequest request = new TestPutRequest
            {
                Id = Guid.NewGuid().ToString("N"),
                Age = 20,
                Name = "tom",
                Price = 888M
            };

            TestPutResponse response = ServiceClient.Execute<TestPutRequest, TestPutResponse>(request);
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine(string.Format("ApiDemoTestPut,Id:{0},Data:{1},Task", response.Id, response.Data));
            Console.ResetColor();

            //TestPutResponse response2 = ServiceClient.Execute<TestPutRequest, TestPutResponse>(request, null, 10000, null, new ResourceAttribute("webapi.ymatou.com", "api/Default/TestPut", Method.PUT, parameterBuildType.UrlQueryString));
        }

        static void ApiDemoTestDelete()
        {
            TestDeleteRequest request = new TestDeleteRequest
            {
                Id = Guid.NewGuid().ToString("N"),
                Age = 20,
                Name = "tom",
                Price = 888M
            };

            TestDeleteResponse response = ServiceClient.Execute<TestDeleteRequest, TestDeleteResponse>(request);

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine(string.Format("ApiDemoTestDelete,Id:{0},Data:{1},Task", response.Id, response.Data));
            Console.ResetColor();
        }
    }
}
